//#include "allocation.h"
#ifndef IMAGE_READ_H_INCLUDED
#define IMAGE_READ_H_INCLUDED

typedef struct header{
	char head[2];
	int height;
	int width;
	int depth;
	int intensity;
}img_header;
img_header img1;
void reintialize_img_header()
{

     img1.height=0;
     img1.width=0;
     img1.depth=0;
     img1.intensity=0;

}
int ***img_read_ppm(char *file_name)
{

      int ***img_array,row,col,d_pth;
      FILE *fp1;
      fp1=fopen(file_name,"r");

	if(fp1==NULL)
	{
		printf("\nerror opening image file\n");
		exit(1);
	}
	fscanf(fp1,"%s\n",img1.head);
	fscanf(fp1,"%d\t",&img1.width);
	fscanf(fp1,"%d\n",&img1.height);
	fscanf(fp1,"%d\n",&img1.intensity);
    img1.depth=3;
	img_array=alloc3Dint(img1.height,img1.width,img1.depth);

		for(row=0;row<img1.height;row++)
		{
			for(col=0;col<img1.width;col++)
			{
			  for(d_pth=0;d_pth<img1.depth;d_pth++)
			  {
				fscanf(fp1,"%d",&img_array[row][col][d_pth]);
			  }
		        }
	        }
	fclose(fp1);
	return img_array;
}
int **img_read_pgm(char *file_name)
{
      int row,col;
      int **img_array;
      FILE *fp1;
      fp1=fopen(file_name,"r");
	if(fp1==NULL)
	{
		printf("error opening image file");
		exit(1);
	}
	fscanf(fp1,"%s\n",img1.head);
	fscanf(fp1,"%d\t",&img1.width);
	fscanf(fp1,"%d\n",&img1.height);
	fscanf(fp1,"%d\n",&img1.intensity);

	img_array=alloc2Dint(img1.height,img1.width);

		for(row=0;row<img1.height;row++)
		{
			for(col=0;col<img1.width;col++)
			{

				fscanf(fp1,"%d",&img_array[row][col]);
			  }

	        }
	        fclose (fp1);
	return img_array;
}
void img_write_ppm(int ***img_array,char *file_name)
{

      FILE *fp1;
      int row,col,depth;
      fp1=fopen(file_name,"w");
	if(fp1==NULL)
	{
		printf("error opening image file");
		exit(1);
	}
	fprintf(fp1,"%s\n","P3");
	fprintf(fp1,"%d\t",img1.width);
	fprintf(fp1,"%d\n",img1.height);
	fprintf(fp1,"%d\n",img1.intensity);

		for(row=0;row<img1.height;row++)
		{
			for(col=0;col<img1.width;col++)
			{
			  for(depth=0;depth<img1.depth;depth++)
			   {
				fprintf(fp1,"%d\n",img_array[row][col][depth]);
			   }

			  }

		  }

	fclose(fp1);
}
void img_write_pgm(int **img_array,char *file_name, int h, int w)
{

      FILE *fp1;
      int row,col;

      fp1=fopen(file_name,"w");
	if(fp1==NULL)
	{
		printf("error opening image file");
		exit(1);
	}
	fprintf(fp1,"%s\n","P2");
	fprintf(fp1,"%d\t",w);
	fprintf(fp1,"%d\n",h);
	fprintf(fp1,"%d\n",255);

		for(row=0;row<h;row++)
		{
			for(col=0;col<w;col++)
			{

				fprintf(fp1,"%d\t",img_array[row][col]);
			}
		    fprintf(fp1,"\n");
		}
	fclose(fp1);

}

void img_write_pgm_f(float **img_array,char *file_name)
{

      FILE *fp1;
      int row,col;
      fp1=fopen(file_name,"w");
	if(fp1==NULL)
	{
		printf("error opening image file");
		exit(1);
	}
	fprintf(fp1,"%s\n",img1.head);
	fprintf(fp1,"%d\t",img1.width);
	fprintf(fp1,"%d\n",img1.height);
	fprintf(fp1,"%d\n",img1.intensity);

		for(row=0;row<img1.height;row++)
		{
			for(col=0;col<img1.width;col++)
			{

				fprintf(fp1,"%f\t",img_array[row][col]);
			}
		    fprintf(fp1,"\n");
		}
	fclose(fp1);

}
#else
#endif
